/**
 * 
 */
package com.lvmama.vst.batis.datasource;

import java.util.Stack;

/**
 * 
 * DataSourceContextHolder
 * 
 * @author baolm
 *
 */
public class DataSourceContextHolder {

    private static final ThreadLocal<Stack<String>> contextHolder = new ThreadLocal<>();

    public static void setDataSource(String dataSourceName) {
        Stack<String> stack = contextHolder.get();
        if (stack == null) {
            stack = new Stack<>();
        }
        stack.add(dataSourceName);
        contextHolder.set(stack);
    }

    public static String getCurrentDataSource() {
        Stack<String> stack = contextHolder.get();
        if (stack == null || stack.isEmpty())
            return null;
        return stack.peek();
    }

    public static boolean isEmptyDataSource() {
        return contextHolder.get() == null || contextHolder.get().isEmpty();
    }

    public static boolean isNotEmptyDataSource() {
        return !isEmptyDataSource();
    }

    public static void clearDataSource() {
        Stack<String> stack = contextHolder.get();
        if (stack == null)
            return;
        stack.pop();
        if (stack.isEmpty())
            contextHolder.remove();
    }

}
